<?php 
require_once 'Captcha/captcha.php'; 

class Utils_Captcha
{
	const NAMESPACE_CAPTCHA = 'Captcha';
	const PREFIX_CAPTCHA = 'captcha';
	
	const PARAM_ID = 'idCaptcha';
	const PARAM_ID_DEFAULT = -1;
	const PARAM_TYPE = 'type';
	const PARAM_TYPE_DEFAULT = 'gif';
	const PARAM_LENGTH = 'stringLength';
	const PARAM_LENGTH_DEFAULT = 4;
	const PARAM_SIZE = 'sizeFont';
	const PARAM_SIZE_DEFAULT = 15;
	
	private static $captchaNamespace = null;
	
	private $captcha = null;

	public function __construct($type=self::PARAM_TYPE_DEFAULT, $stringLength=self::PARAM_LENGTH_DEFAULT, $sizeFont=self::PARAM_SIZE_DEFAULT, $id=self::PARAM_ID_CAPTCHA_DEFAULT) {
		$captcha = new captcha($type);
		$this->setCaptcha($captcha);
		$this->setStringLenght($stringLength);
		$this->setFont(Manager::getPathDefaultCaptchaFont() , $sizeFont);
		$this->saveCaptchaString($id);
	}
	
	private function saveCaptchaString($id) {
		$captchaNamespace = self::getCaptchaNamespace();
		//if ($id != self::FAKE_ID && !isset($captchaNamespace->$id))
		//{
			$captchaNamespace->$id = $this->getRandString();
		//}
	}

	/**
	 * Modifie le captcha
	 * @param Captcha $captcha le nouveau captcha
	 */
	private function setCaptcha(Captcha $captcha) {
		$this->captcha = $captcha;
	} 
	
	/**
	 * Retourne le captcha
	 * @return Captcha captcha
	 */
	private function getCaptcha() {
		return $this->captcha;
	}

	/**
	 * Modifie le captchaNamespace
	 * @param Zend_Session_Namespace $captchaNamespace le nouveau captchaNamespace
	 */
	private static function setCaptchaNamespace(Zend_Session_Namespace $captchaNamespace) {
		self::$captchaNamespace = $captchaNamespace;
	} 
	
	/**
	 * Retourne le captchaNamespace
	 * @return Zend_Session_Namespace captchaNamespace
	 */
	private static function getCaptchaNamespace() {
		if (is_null(self::$captchaNamespace)) {
			self::$captchaNamespace = new Zend_Session_Namespace(self::NAMESPACE_CAPTCHA);
		}
		return self::$captchaNamespace;
	}

    /**
     * Calls all methods from the captcha
     */
    public function __call($method, array $options)
    {
        if (method_exists($this->getCaptcha(), $method)) {
            return call_user_func_array(array($this->getCaptcha(), $method), $options);
        }
        throw new Exception("Unknown method '" . $method . "' called!");
    }
	
	private static function removeCaptchaString($id) {
		$captchaNamespace = self::getCaptchaNamespace();
		unset($captchaNamespace->$id);
	}
	
	private static function getCaptchaString($id) {
		$captchaNamespace = self::getCaptchaNamespace();
		return $captchaNamespace->$id;
	}
	
	public static function isValid($string, $id) {
		$captchaNamespace = self::getCaptchaNamespace();
		$captchaString = self::getCaptchaString($id);
		self::removeCaptchaString($id);
		return strcasecmp($captchaString, $string) == 0;
	}
	
	public static function getNewId() {
		$numId = 0;
		$captchaNamespace = self::getCaptchaNamespace();
		
		do {
			$newId = self::PREFIX_CAPTCHA.$numId;
			$numId++;
		}
		while (isset($captchaNamespace->$newId));
		
		return $newId;
	}
	
	public static function getCurrentIdCaptcha() {
		$frontController = Zend_Controller_Front::getInstance();
		$idCaptcha = $frontController->getRequest()->getParam(Utils_Captcha::PARAM_ID, Utils_Captcha::PARAM_ID_DEFAULT);
		return $idCaptcha;
	}
	
}